﻿Namespace SetupDatastore
    Public Class Manager
        Public Shared Function EnsureLocalDataBaseIsUpToDate(ByVal owner As Form) As Boolean
            'make sure we know the path and password
            If Not Framework.Data.Setup.IsLocalDatabaseSetup Then
                Dim clsRes As Result = GetPathAndPassword(owner)
                If clsRes Is Nothing Then Return False
                Framework.Data.Environment.SetActiveDatabase(clsRes.Path, clsRes.Password)
            End If
            'ok, so we already have path and password. Make sure it's up to date
            If Not Manager.ValidateCreateOrUpgradeDb(owner) Then Return False

            If Framework.Data.Setup.DoesLocalDatabaseNeedUpgrade Then
                Dim strWhyNot As String = Nothing
                If Not Framework.Data.Setup.CanUpgradeFromCurrentVersion(strWhyNot) Then
                    Select Case MessageBox.Show(owner, strWhyNot & vbNewLine & vbNewLine & "Continue by erasing your local database and replacing with new empty database??", "Upgrade problem", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                        Case DialogResult.OK
                            Select Case MessageBox.Show(owner, "Are you really, really sure?!?!?!? This is probably a bug and you will loose all your buddies, shares, downloads, etc!!!", "REALLY! READ CAREFULLY!!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                                Case DialogResult.OK
                                    Framework.Data.Setup.CreateBackup()
                                    Framework.Data.Environment.CreateLocalDatabase(Framework.Data.Environment.DBPath, Framework.Data.Environment.DBPassword)
                                Case Else
                                    Return False
                            End Select
                        Case Else
                            Return False
                    End Select
                Else
                    Dim aIcons As New Generic.List(Of Image)
                    aIcons.Add(Framework.My.Resources.DB1.ToBitmap)
                    aIcons.Add(Framework.My.Resources.DB2.ToBitmap)
                    aIcons.Add(Framework.My.Resources.DB3.ToBitmap)
                    aIcons.Add(Framework.My.Resources.DB4.ToBitmap)

                    Using m As New Framework.Message("Upgrading local database...", aIcons.ToArray)
                        Framework.Data.Setup.UpgradeLocalDatabase(False)
                        m.SetMessage("Local database upgrade completed!")
                        m.SetRightIconSet(New System.Drawing.Image() {aIcons(0)})
                        Threading.Thread.Sleep(1000)
                    End Using
                End If
            End If
            IsoDBSettings.DBPass = Framework.Data.Environment.DBPassword
            IsoDBSettings.DBPath = Framework.Data.Environment.DBPath
            IsoDBSettings.Save()
            Return True
        End Function

        Private Shared Function GetPathAndPassword(ByVal owner As IWin32Window) As Result
            Return FGetDatastorePathAndPassword.CreateAndShowDialog(owner, Framework.Data.Environment.DBPath, Framework.Data.Environment.DBPassword)
        End Function

        Public Shared Function ValidateCreateOrUpgradeDb(ByVal owner As Form) As Boolean
            Return ValidateCreateOrUpgradeDb(owner, Framework.Data.Environment.DBPath, Framework.Data.Environment.DBPassword)
        End Function

        Public Shared Function ValidateCreateOrUpgradeDb(ByVal owner As Form, ByVal path As String, ByVal password As String) As Boolean
            Dim strCaption As String = "Local database - B2B"
            If owner IsNot Nothing Then strCaption = owner.Text
            Select Case Framework.Data.Environment.TestConnection(path, password)
                Case Framework.Data.Environment.TestConnectionResult.FileDoesNotExist
                    Select Case MessageBox.Show(owner, "File does not exist. Continue and create a new empty database?", strCaption, MessageBoxButtons.YesNo)
                        Case Windows.Forms.DialogResult.Yes
                            Framework.Data.Environment.CreateLocalDatabase(path, password)
                            Return True
                        Case Else
                            Return False
                    End Select
                Case Framework.Data.Environment.TestConnectionResult.OK
                    'all's well
                    Return True
                Case Framework.Data.Environment.TestConnectionResult.UnknownError
                    Select Case MessageBox.Show(owner, "Unknown error connection to the database! Continue and replace with new empty database?", strCaption, MessageBoxButtons.YesNo)
                        Case Windows.Forms.DialogResult.Yes
                            Framework.Data.Environment.CreateLocalDatabase(path, password)
                            Return True
                        Case Else
                            Return False
                    End Select
                Case Framework.Data.Environment.TestConnectionResult.InvalidDbFormat
                    Select Case MessageBox.Show(owner, "The database file seems to be in an older file format! Continue and attempt to upgrade it?", strCaption, MessageBoxButtons.YesNo)
                        Case Windows.Forms.DialogResult.Yes
                            Framework.Data.Setup.UpgradeDbFileFormat(path, password)
                            Return True
                        Case Else
                            Return False
                    End Select
                Case Else
                    Throw New NotImplementedException
            End Select
        End Function

    End Class

    Friend Class Result
        Public Password As String
        Public Path As String
    End Class

End Namespace
